package com.wisdomparking.config.mvc;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ResultGenerator {

	@SuppressWarnings("unchecked")
	public static <T> Result<T> genResult(int code, T data, String message) {
		Result<T> result = Result.newInstance();
		result.setCode(code);
		
		if(null==data) {
			result.setData((T) "null");
		}
		else {
			result.setData(data);
		}
		
		result.setMessage(message);
		if (log.isDebugEnabled()) {
			log.debug("generate rest result:{}", result);
		}
		return result;
	}

	public static <T> Result<T> genSuccessResult(T data) {

		return genResult(1, data, "success");
	}

	public static <T> Result<T> genErrorResult(String message) {

		return genResult(0, null, message);
	}

	public static <T> Result<T> genErrorResult(ErrorCode error) {

		return genErrorResult(error.getMessage());
	}

	@SuppressWarnings("rawtypes")
	public static Result genSuccessResult() {
		return genSuccessResult(null);
	}

}
